/*
 * Copyright 2009-2010  Geovise BVBA, QMINO BVBA
 *
 * This file is part of GeoLatte Mapserver.
 *
 * GeoLatte Mapserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GeoLatte Mapserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoLatte Mapserver.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.geolatte.mapserver.app;


import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.geolatte.mapserver.servlet.WMSServlet;

/**
 * This class allows you to run the map server as a stand-alone application.
 * <p/>
 * This is intended for quick tests and should not be used in production
 * systems.
 *
 * @author Karel Maesen, Geovise BVBA
 */
public class JettyServer {
    private static Server server = null;

    public static void main(String[] args) throws Exception {
        start();
        server.join();
    }

    public static void start() throws Exception {
        if (server != null && !server.isStarting() && !server.isStarted()) {
            server.start();
        } else {
            server = new Server(8090);
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);

            context.addServlet(new ServletHolder(new WMSServlet()), "/wms");

            server.start();
        }
    }

    public static void stop() throws Exception {
        if (server != null) {
            if(server.isStopping())
                return;
            if(server.isStarted() || server.isStarting())
                server.stop();
        }
    }
}
